/* eslint-disable */
import { getRowIdentity } from 'element-ui/packages/table/src/util'

require('script-loader!file-saver');
require('script-loader!@/excel/Blob');
require('script-loader!xlsx-style/dist/xlsx.core.min');
const ExcelJS = require('exceljs');
function generateArray(table) {
  var out = [];
  var rows = table.querySelectorAll('tr');
  var ranges = [];
  for (var R = 0; R < rows.length; ++R) {
    var outRow = [];
    var row = rows[R];
    var columns = row.querySelectorAll('td');
    for (var C = 0; C < columns.length; ++C) {
      var cell = columns[C];
      var colspan = cell.getAttribute('colspan');
      var rowspan = cell.getAttribute('rowspan');
      var cellValue = cell.innerText;
      if (cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue;

      //Skip ranges
      ranges.forEach(function (range) {
        if (R >= range.s.r && R <= range.e.r && outRow.length >= range.s.c && outRow.length <= range.e.c) {
          for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null);
        }
      });

      //Handle Row Span
      if (rowspan || colspan) {
        rowspan = rowspan || 1;
        colspan = colspan || 1;
        ranges.push({s: {r: R, c: outRow.length}, e: {r: R + rowspan - 1, c: outRow.length + colspan - 1}});
      }
      ;

      //Handle Value
      outRow.push(cellValue !== "" ? cellValue : null);

      //Handle Colspan
      if (colspan) for (var k = 0; k < colspan - 1; ++k) outRow.push(null);
    }
    out.push(outRow);
  }
  return [out, ranges];
};

function datenum(v, date1904) {
  if (date1904) v += 1462;
  var epoch = Date.parse(v);
  return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
}

function sheet_from_array_of_arrays(data, opts) {
  var ws = {};
  var range = {s: {c: 10000000, r: 10000000}, e: {c: 0, r: 0}};
  for (var R = 0; R != data.length; ++R) {
    for (var C = 0; C != data[R].length; ++C) {
      if (range.s.r > R) range.s.r = R;
      if (range.s.c > C) range.s.c = C;
      if (range.e.r < R) range.e.r = R;
      if (range.e.c < C) range.e.c = C;
      var cell = {v: data[R][C]};
      if (cell.v == null) continue;
      var cell_ref = XLSX.utils.encode_cell({c: C, r: R});

      if (typeof cell.v === 'number') cell.t = 'n';
      else if (typeof cell.v === 'boolean') cell.t = 'b';
      else if (cell.v instanceof Date) {
        cell.t = 'n';
        cell.z = XLSX.SSF._table[14];
        cell.v = datenum(cell.v);
      }
      else cell.t = 's';

      ws[cell_ref] = cell;
    }
  }
  if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
  return ws;
}

function Workbook() {
  if (!(this instanceof Workbook)) return new Workbook();
  this.SheetNames = [];
  this.Sheets = {};
}

function s2ab(s) {
  var buf = new ArrayBuffer(s.length);
  var view = new Uint8Array(buf);
  for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  return buf;
}

export function export_table_to_excel(id) {
  var theTable = document.getElementById(id);
  console.log('a')
  var oo = generateArray(theTable);
  var ranges = oo[1];

  /* original data */
  var data = oo[0];
  var ws_name = "SheetJS";
  console.log(data);

  var wb = new Workbook(), ws = sheet_from_array_of_arrays(data);

  /* add ranges to worksheet */
  // ws['!cols'] = ['apple', 'banan'];
  ws['!merges'] = ranges;

  /* add worksheet to workbook */
  wb.SheetNames.push(ws_name);
  wb.Sheets[ws_name] = ws;

  var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: false, type: 'binary'});

  saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), "test.xlsx")
}

function formatJson(jsonData) {
  console.log(jsonData)
}
export function export_json_to_excel(title1,date, th, jsonData, defaultTitle,merges1) {

  /* original data */

  var data = jsonData;

  data.unshift(th);
  data.unshift(date);
  data.unshift(title1);
   var ws_name = "SheetJS";

  var wb = new Workbook(), ws = sheet_from_array_of_arrays(data);


  /* add worksheet to workbook */
  wb.SheetNames.push(ws_name);
  wb.Sheets[ws_name] = ws;
  var dataInfo = wb.Sheets[wb.SheetNames[0]];

  ws['!merges']=[
    {s:{r:0,c:0},e:{r:0,c:9}},
    {s:{r:1,c:0},e:{r:1,c:9}}
  ];


  const borderAll= {
    top:{
      style:'thin'
    },
    bottom:{
      style:'thin'
    },
    left:{
      style:'thin'
    },
    right:{
      style:'thin'
    }
  };
  for (var i in dataInfo) {
    if (i == '!ref' || i == 'merges' || i == '!cols' || i == 'A1' || i == 'B1' || i == 'C1' || i == 'D1' || i == 'E1' || i == 'F1' || i == 'G1'|| i == 'H1' || i == 'I1' || i == 'J1'  || i == 'L1'||i=='K1'||i == 'A2' || i == 'B2' || i == 'C2' || i == 'D2' || i == 'E2' || i == 'F2' || i == 'G2'|| i == 'H2' || i == 'I2' || i == 'J2'  || i == 'L2'||i=='K2') {
    }
  else {
      dataInfo[i + ''].s = {
        border: borderAll,//边框
        colWidth: dataInfo.toString().length,
        font: {
          name: "宋体", //字体
           sz: 12, //字体大小
           color: {
             rgb: "000000"
           },
           italic: false,
           underline: false,
        },
        alignment: {//居中和换行
          horizontal: "center",
          vertical: "center"
        }
      }
    }
  }
  // 去掉标题边框
  let arr = ["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1", "R1", "S1", "T1", "U1", "V1", "W1", "X1", "Y1", "Z1"];
  arr.some(function (v) {
    //let a = arr.split(':')
    if (v == arr[1]) {
      dataInfo[v].s = {}
      return true;
    } else {
      dataInfo[v].s = {}
    }
  });
  dataInfo["A1"].s = {
    font: {
      name: '黑体',
      sz: 22,
      color: { rgb: "000000" },
      bold: false,
      italic: false,
      underline: false
    },
    alignment: {
      horizontal: "center",
      vertical: "center"
    },
    // fill: {
    //   fgColor: {rgb: "008000"},
    // },
  };

  var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: false, type: 'binary'});
  var title = defaultTitle || '列表'
  saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), title + ".xlsx");


  }


export function export_json_to_excelaccept(title1, title2, title3, title4, title5, jsonData, defaultTitle, end1, end2, end3) {

  /* original data */

  var data = jsonData;

  data.unshift(title5);
  data.unshift(title4);
  data.unshift(title3);
  data.unshift(title2);
  data.unshift(title1);
  data.push(end1);
  data.push(end2);
  data.push(end3);
  var ws_name = "SheetJS";

  var wb = new Workbook(), ws = sheet_from_array_of_arrays(data);


  /* add worksheet to workbook */
  wb.SheetNames.push(ws_name);
  wb.Sheets[ws_name] = ws;
  var dataInfo = wb.Sheets[wb.SheetNames[0]];

  ws['!merges']=[
    {s:{r:0,c:0},e:{r:0,c:14}},

    {s:{r:1,c:0},e:{r:1,c:2}}, {s:{r:1,c:5},e:{r:1,c:8}}, {s:{r:1,c:13},e:{r:1,c:14}},

    {s:{r:2,c:0},e:{r:2,c:2}}, {s:{r:2,c:4},e:{r:2,c:6}}, {s:{r:2,c:8},e:{r:2,c:10}}, {s:{r:2,c:12},e:{r:2,c:14}},

    {s:{r:3,c:0},e:{r:4,c:0}}, {s:{r:3,c:1},e:{r:4,c:1}}, {s:{r:3,c:2},e:{r:4,c:2}}, {s:{r:3,c:3},e:{r:4,c:3}},
    {s:{r:3,c:4},e:{r:4,c:4}}, {s:{r:3,c:5},e:{r:4,c:5}}, {s:{r:3,c:14},e:{r:4,c:14}}, {s:{r:3,c:6},e:{r:3,c:7}},
    {s:{r:3,c:8},e:{r:3,c:9}}, {s:{r:3,c:10},e:{r:3,c:11}}, {s:{r:3,c:12},e:{r:3,c:13}},

    // {s:{r:7,c:0},e:{r:7,c:1}}, {s:{r:7,c:2},e:{r:7,c:14}},
    //
    // {s:{r:8,c:0},e:{r:8,c:2}}, {s:{r:8,c:5},e:{r:8,c:7}}, {s:{r:8,c:10},e:{r:8,c:14}},

  ];

  const borderAll= {
    top:{
      style:'thin'
    },
    bottom:{
      style:'thin'
    },
    left:{
      style:'thin'
    },
    right:{
      style:'thin'
    }
  };
  for (var i in dataInfo) {
    if (i == '!ref' || i == 'merges' || i == '!cols' || i == 'A1' || i == 'B1' || i == 'C1' || i == 'D1' || i == 'E1' || i == 'F1' || i == 'G1'|| i == 'H1' || i == 'I1' || i == 'J1'  || i == 'L1'||i=='K1'||i == 'A2' || i == 'B2' || i == 'C2' || i == 'D2' || i == 'E2' || i == 'F2' || i == 'G2'|| i == 'H2' || i == 'I2' || i == 'J2'  || i == 'L2'||i=='K2') {
    }
    else {
      dataInfo[i + ''].s = {
        border: borderAll,//边框
        colWidth: dataInfo.toString().length,
        font: {
          name: "宋体", //字体
          sz: 12, //字体大小
          color: {
            rgb: "000000"
          },
          italic: false,
          underline: false,
        },
        alignment: {//居中和换行
          horizontal: "center",
          vertical: "center"
        }
      }
    }
  }
  // 去掉标题边框
  let arr = ["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1", "R1", "S1", "T1", "U1", "V1", "W1", "X1", "Y1", "Z1",
    "A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2", "I2", "J2", "K2", "L2", "M2", "N2", "O2", "P2", "Q2", "R2", "S2", "T2", "U2", "V2", "W2", "X2", "Y2", "Z2",
    "A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3", "I3", "J3", "K3", "L3", "M3", "N3", "O3", "P3", "Q3", "R3", "S3", "T3", "U3", "V3", "W3", "X3", "Y3", "Z3"
  ];
  arr.some(function (v) {
    //let a = arr.split(':')
    if (v == arr[1]) {
      dataInfo[v].s = {}
      return true;
    } else {
      dataInfo[v].s = {}
    }
  });
  dataInfo["A1"].s = {
    font: {
      name: '黑体',
      sz: 22,
      color: { rgb: "000000" },
      bold: false,
      italic: false,
      underline: false
    },
    alignment: {
      horizontal: "center",
      vertical: "center"
    },
    // fill: {
    //   fgColor: {rgb: "008000"},
    // },
  };

  var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: false, type: 'binary'});
  var title = defaultTitle || '列表'
  saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), title + ".xlsx");

}


export function export_json_to_excelreceive(title1, title2, title3, title4, title5, jsonData, defaultTitle, end1, end2) {

  /* original data */

  var data = jsonData;

  data.unshift(title5);
  data.unshift(title4);
  data.unshift(title3);
  data.unshift(title2);
  data.unshift(title1);
  data.push(end1);
  data.push(end2);

  var ws_name = "SheetJS";

  var wb = new Workbook(), ws = sheet_from_array_of_arrays(data);


  /* add worksheet to workbook */
  wb.SheetNames.push(ws_name);
  wb.Sheets[ws_name] = ws;
  var dataInfo = wb.Sheets[wb.SheetNames[0]];

  ws['!merges']=[
    {s:{r:0,c:0},e:{r:0,c:10}},

    {s:{r:1,c:0},e:{r:1,c:2}}, {s:{r:1,c:4},e:{r:1,c:6}},

    {s:{r:2,c:0},e:{r:2,c:2}}, {s:{r:2,c:8},e:{r:2,c:10}},

    {s:{r:3,c:0},e:{r:4,c:0}}, {s:{r:3,c:1},e:{r:4,c:1}}, {s:{r:3,c:2},e:{r:4,c:2}}, {s:{r:3,c:3},e:{r:4,c:3}},
    {s:{r:3,c:10},e:{r:4,c:10}}, {s:{r:3,c:4},e:{r:3,c:5}}, {s:{r:3,c:6},e:{r:3,c:7}}, {s:{r:3,c:8},e:{r:3,c:9}},

  ];


  const borderAll= {
    top:{
      style:'thin'
    },
    bottom:{
      style:'thin'
    },
    left:{
      style:'thin'
    },
    right:{
      style:'thin'
    }
  };
  for (var i in dataInfo) {
    if (i == '!ref' || i == 'merges' || i == '!cols' || i == 'A1' || i == 'B1' || i == 'C1' || i == 'D1' || i == 'E1' || i == 'F1' || i == 'G1'|| i == 'H1' || i == 'I1' || i == 'J1'  || i == 'L1'||i=='K1'||i == 'A2' || i == 'B2' || i == 'C2' || i == 'D2' || i == 'E2' || i == 'F2' || i == 'G2'|| i == 'H2' || i == 'I2' || i == 'J2'  || i == 'L2'||i=='K2') {
    }
    else {
      dataInfo[i + ''].s = {
        border: borderAll,//边框
        colWidth: dataInfo.toString().length,
        font: {
          name: "宋体", //字体
          sz: 12, //字体大小
          color: {
            rgb: "000000"
          },
          italic: false,
          underline: false,
        },
        alignment: {//居中和换行
          horizontal: "center",
          vertical: "center"
        }
      }
    }
  }
  // 去掉标题边框
  let arr = ["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1", "R1", "S1", "T1", "U1", "V1", "W1", "X1", "Y1", "Z1"];
  arr.some(function (v) {
    //let a = arr.split(':')
    if (v == arr[1]) {
      dataInfo[v].s = {}
      return true;
    } else {
      dataInfo[v].s = {}
    }
  });
  dataInfo["A1"].s = {
    font: {
      name: '黑体',
      sz: 22,
      color: { rgb: "000000" },
      bold: false,
      italic: false,
      underline: false
    },
    alignment: {
      horizontal: "center",
      vertical: "center"
    },
    // fill: {
    //   fgColor: {rgb: "008000"},
    // },
  };

  var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: false, type: 'binary'});
  var title = defaultTitle || '列表'
  saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), title + ".xlsx");


}


export function export_json_to_excelreturn(title1, title2, title3, title4, jsonData, defaultTitle, end1, end2) {

  /* original data */

  var data = jsonData;

  data.unshift(title4);
  data.unshift(title3);
  data.unshift(title2);
  data.unshift(title1);
  data.push(end1);
  data.push(end2);
  var ws_name = "SheetJS";

  var wb = new Workbook(), ws = sheet_from_array_of_arrays(data);


  /* add worksheet to workbook */
  wb.SheetNames.push(ws_name);
  wb.Sheets[ws_name] = ws;
  var dataInfo = wb.Sheets[wb.SheetNames[0]];

  ws['!merges']=[
    {s:{r:0,c:0},e:{r:0,c:6}},

    {s:{r:1,c:0},e:{r:1,c:1}}, {s:{r:1,c:3},e:{r:1,c:4}},

    {s:{r:2,c:0},e:{r:2,c:1}}, {s:{r:2,c:5},e:{r:2,c:6}},

  ];


  const borderAll= {
    top:{
      style:'thin'
    },
    bottom:{
      style:'thin'
    },
    left:{
      style:'thin'
    },
    right:{
      style:'thin'
    }
  };
  for (var i in dataInfo) {
    if (i == '!ref' || i == 'merges' || i == '!cols' || i == 'A1' || i == 'B1' || i == 'C1' || i == 'D1' || i == 'E1' || i == 'F1' || i == 'G1'|| i == 'H1' || i == 'I1' || i == 'J1'  || i == 'L1'||i=='K1'||i == 'A2' || i == 'B2' || i == 'C2' || i == 'D2' || i == 'E2' || i == 'F2' || i == 'G2'|| i == 'H2' || i == 'I2' || i == 'J2'  || i == 'L2'||i=='K2') {
    }
    else {
      dataInfo[i + ''].s = {
        border: borderAll,//边框
        colWidth: dataInfo.toString().length,
        font: {
          name: "宋体", //字体
          sz: 12, //字体大小
          color: {
            rgb: "000000"
          },
          italic: false,
          underline: false,
        },
        alignment: {//居中和换行
          horizontal: "center",
          vertical: "center"
        }
      }
    }
  }
  // 去掉标题边框
  let arr = ["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1", "K1", "L1", "M1", "N1", "O1", "P1", "Q1", "R1", "S1", "T1", "U1", "V1", "W1", "X1", "Y1", "Z1"];
  arr.some(function (v) {
    //let a = arr.split(':')
    if (v == arr[1]) {
      dataInfo[v].s = {}
      return true;
    } else {
      dataInfo[v].s = {}
    }
  });
  dataInfo["A1"].s = {
    font: {
      name: '黑体',
      sz: 22,
      color: { rgb: "000000" },
      bold: false,
      italic: false,
      underline: false
    },
    alignment: {
      horizontal: "center",
      vertical: "center"
    },
    // fill: {
    //   fgColor: {rgb: "008000"},
    // },
  };

  var wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: false, type: 'binary'});
  var title = defaultTitle || '列表'
  saveAs(new Blob([s2ab(wbout)], {type: "application/octet-stream"}), title + ".xlsx");


}
